/**
 * localeIconsOptions.js
 * Copyright (C) 2008-2011 Tommi Rautava
 * 
 * This file is part of Popmungo.
 *
 * Popmungo is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Popmungo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

org.mozdev.popmungo.localeIconsOptions = {
	
	treeView: null,
	
	onLoad: function localeIconsOptionsOnLoad() {
		try {
			var bundle = document.getElementById('popmungo.locale.locales.stringbundle');
	
			var children = org.mozdev.popmungo.prefs.getChildList("extensions.popmungo.options.locales.icon.");
			children.sort(function (a, b) { 
				return (parseInt(a) - parseInt(b));
			});
	
			org.mozdev.popmungo.logger.debug("children="+ children.length);
			
			this.treeView = new org.mozdev.popmungo.classes.LocaleIconsTreeView;
			
			for (var i = 0, child; child = children[i]; i++) {
				var prefName = "extensions.popmungo.options.locales.icon." + child;
				var url = org.mozdev.popmungo.prefs.getPref(prefName, "");
				
				var matchResult = prefName.match(/\.(\d+)\./);
				var localeTypeId = matchResult[1];
				var localeTypeName = bundle.getString("locale."+ localeTypeId);
				
				var row = new org.mozdev.popmungo.classes.LocaleIconsTreeItem(localeTypeId, localeTypeName, url);
				this.treeView.appendItem(row);
			}
			
			var tree = document.getElementById("locales.localeIcon.tree");
			tree.view = this.treeView;
			
			this.localeIconFeaturesOnCommand();
		}
		catch (err) {
			org.mozdev.popmungo.logger.logError(err);
		}
		
		return true;
	},
	
	
	localeIconFeaturesOnCommand:
	function localeIconFeaturesOnCommand() {
		var cb = document.getElementById("locales.icons.enabled.checkbox");
		var disabled = !cb.checked || cb.disabled;
		
		document.getElementById("locales.localeIcon.tree").disabled = disabled;
		
		this.localeIconSelectionChanged();
		
		return true;
	},
	

    localeIconSelectionChanged: 
    function localeIconSelectionChanged() {
		var cb = window.document.getElementById("locales.icons.enabled.checkbox");
		var disabled = !cb.checked || cb.disabled;

		var localeTypeNameTextbox    = window.document.getElementById("localeTypeName.textbox");
    	var iconUrlTextbox           = window.document.getElementById("localeIconUrl.textbox");
    	var localeIconUrlSetButton   = window.document.getElementById("localeIconUrl.set.button");
    	var localeIconUrlResetButton = window.document.getElementById("localeIconUrl.reset.button");

    	if (this.treeView.selection) {
    		var setEnabled = false;
    		var resetEnabled = false;
	    	var row = this.treeView.selection.currentIndex;
	    	
	    	if (row >= 0) {
		    	var item = this.treeView.getItemAtIndex(row);
		    	
		    	if (item) {
			    	localeTypeNameTextbox.value = item.localeTypeName;
			    	iconUrlTextbox.value = item.localeIconUrl;
			    	setEnabled = true;
	
			    	var prefName = org.mozdev.popmungo.prefKeys.GetLocaleIconUrlPrefKey(item.localeTypeId);
			    	resetEnabled = org.mozdev.popmungo.prefs.prefHasUserValue(prefName);    	
		    	}
	    	}

			localeTypeNameTextbox.disabled    = disabled || !setEnabled;
	    	iconUrlTextbox.disabled           = disabled || !setEnabled;
			localeIconUrlSetButton.disabled   = disabled || !setEnabled;
	    	localeIconUrlResetButton.disabled = disabled || !resetEnabled;
}
    	else
    	{
    		localeTypeNameTextbox.value = "";
    		iconUrlTextbox.value = "";

			localeTypeNameTextbox.disabled    = true;
	    	iconUrlTextbox.disabled           = true;
			localeIconUrlSetButton.disabled   = true;
	    	localeIconUrlResetButton.disabled = true;
}
    },


	resetLocaleIconsTree: function resetLocaleIconsTree() {
    	var tb;

    	tb = document.getElementById("localeTypeName.textbox");
    	tb.value = "";
    	tb.disabled = true;

    	tb = document.getElementById("localeIconUrl.textbox");
    	tb.value = "";
    	tb.disabled = true;

    	document.getElementById("localeIconUrl.set.button").disabled = true;
    	document.getElementById("localeIconUrl.reset.button").disabled = true;

		var tree = document.getElementById("locales.localeIcon.tree");
		tree.view = this.treeView;
	},
	
	
	setIconUrl: function setIconUrl() {
    	var row = this.treeView.selection.currentIndex;
    	var item = this.treeView.getItemAtIndex(row);

    	var localeIconUrlTextbox = document.getElementById("localeIconUrl.textbox");
    	var iconUrl = localeIconUrlTextbox.value;
    	org.mozdev.popmungo.logger.debug(item.localeTypeId +":"+ iconUrl);
    	item.localeIconUrl = iconUrl;
    	
    	// Save preference
    	var prefName = org.mozdev.popmungo.prefKeys.GetLocaleIconUrlPrefKey(item.localeTypeId);
    	org.mozdev.popmungo.prefs.setPref(prefName, iconUrl.toString());

		this.resetLocaleIconsTree();
	},	
	

	resetIconUrl: function resetIconUrl() {
    	var row = this.treeView.selection.currentIndex;
    	var item = this.treeView.getItemAtIndex(row);
	    
	    // Reset preference	
    	var prefName = org.mozdev.popmungo.prefKeys.GetLocaleIconUrlPrefKey(item.localeTypeId);
    	org.mozdev.popmungo.prefs.clearUserPref(prefName);

	    item.localeIconUrl = org.mozdev.popmungo.prefs.getPref(prefName, "");

		this.resetLocaleIconsTree();
	}
	
};

org.mozdev.popmungo.logger.log("org.mozdev.popmungo.localeIconsOptions loaded");

//EOF